package com.qiaofang.applet.common.context;

/**
 * 上下文持有者，提供静态方法获取上下文对象
 *
 * @author yanhai
 */
public class QfContextHolder {

    private final static String MODE_THREAD_LOCAL = "THREAD_LOCAL";

    private static String providerName = MODE_THREAD_LOCAL;

    private static QfContextProvider provider;

    static {
        initialize();
    }

    /**
     * 获取上下文对象
     *
     * @return
     */
    public static QfContext getContext() {
        return provider.getContext();
    }

    /**
     * 修改上下文对象
     *
     * @param context
     */
    public static void setContext(QfContext context) {
        provider.setContext(context);
    }

    /**
     * 初始化
     */
    private static void initialize() {
        if (providerName.equals(MODE_THREAD_LOCAL)) {
            provider = new ThreadLocalQfContextProvider();
        } else {
            throw new IllegalArgumentException();
        }
    }

    /**
     * 修改提供者
     *
     * @param providerName
     */
    public static void setStrategyName(String providerName) {
        QfContextHolder.providerName = providerName;
        initialize();
    }
}
